package com.example.mptnet;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.mptnet.Interfaces.API;
import com.example.mptnet.Models.Items;
import com.example.mptnet.Models.User;

import java.util.ArrayList;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class Worker extends Fragment {
    SharedPreferences settings;
    Retrofit retrofit;
    API api;
    User user = new User();
    ArrayList<Items> items = new ArrayList<>();
    AutoCompleteTextView autoCompleteTextView;
    String UID;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_worker, container, false);
        settings = getContext().getSharedPreferences("SETTINGS", Context.MODE_PRIVATE);
        retrofit = Builder.request();
        api = retrofit.create(API.class);
        autoCompleteTextView = view.findViewById(R.id.items);
        UID = settings.getString("UID","");
        Call<User> calluser = api.GetUser(UID);
        calluser.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                    if(response.isSuccessful())
                        user = response.body();
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
        Call<ArrayList<Items>> callitems = api.GetItems();
        callitems.enqueue(new Callback<ArrayList<Items>>() {
            @Override
            public void onResponse(Call<ArrayList<Items>> call, Response<ArrayList<Items>> response) {
                if (response.isSuccessful()){
                    items = response.body();
                    ArrayList<String> list_string = new ArrayList<>();
                    for (int i = 0; i < items.size(); i++) {
                        list_string.add(items.get(i).getTitle());
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,list_string);
                    adapter.setDropDownViewResource(androidx.appcompat.R.layout.support_simple_spinner_dropdown_item);
                    autoCompleteTextView.setAdapter(adapter);
                }
            }

            @Override
            public void onFailure(Call<ArrayList<Items>> call, Throwable t) {
                Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
        view.findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!user.role.equals("User")){
                    Toast.makeText(getContext(), "Вы не являетесь пользователем, вам не нужно повышать роль", Toast.LENGTH_LONG).show();
                    return;
                }
                else{
                    Call<ResponseBody> callupdaterole = api.updateRole(UID,autoCompleteTextView.getText().toString());
                    callupdaterole.enqueue(new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            if(response.isSuccessful()){
                                Toast.makeText(getContext(), "Ваши данные обновлены и добавлен новый предмет", Toast.LENGTH_LONG).show();
                                getFragmentManager().beginTransaction().replace(R.id.fragment_container, new Profile()).commit();
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable t) {
                            Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        });
        return view;
    }
}